package main

import (
	"fmt"
	"github.com/gorilla/websocket"
	"log"
	"math/rand"
	"mygoedu/day11/socket"
	"net/http"
	"time"
)

func main() {
	dialer := &websocket.Dialer{}
	header := http.Header{
		"name": []string{"tom", "jerry"},
	}
	conn, resp, err := dialer.Dial("ws://127.0.0.1:8080/add", header)
	socket.CheckError(err)
	fmt.Println("websocket response header:")
	for key, value := range resp.Header {
		fmt.Printf("%s:%v\n", key, value)
	}

	rand.Seed(time.Now().Unix())
	defer conn.Close()
	for {
		request := socket.Request{
			A: rand.Intn(100),
			B: rand.Intn(100),
		}
		err = conn.WriteJSON(request)
		socket.CheckError(err)

		var response socket.Response
		err = conn.ReadJSON(&response)
		socket.CheckError(err)
		log.Printf("response sum=%d\n", response.Sum)
		time.Sleep(1 * time.Second)
	}
}
